package ute.mvc.controller.portal.admin;

import java.util.ArrayList;
import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import ute.mvc.model.Subject;
import ute.mvc.service.SubjectService;
import ute.mvc.util.JsonObject;

@Controller
@RequestMapping(value = { "/portal/subject" })
public class SubjectController {

	@Autowired
	SubjectService subjectService;

	/**
	 * Get all subjects from database
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = { "/list", "/" })
	public String getAllSubject(Model model) {

		return "subject_list";
	}

	/**
	 * Add new subject
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String getAddSubject(Model model) {

		model.addAttribute("subject", new Subject());
		return "subject_add";
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String addSubject(@Valid @ModelAttribute Subject subject,
			BindingResult result, Model model) {

		if (result.hasErrors())
			return "subject_add";
		Subject existSubject = subjectService.getSubjectById(subject
				.getSubjectId());
		if (existSubject != null) {
			result.rejectValue("subjectID", "subjectIdExist");
			return "subject_add";
		}

		subjectService.addSubject(subject);
		return "redirect:list";
	}

	/**
	 * Edit subject information
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
	public String getEditSubject(@PathVariable String id, Model model) {

		Subject subject = subjectService.getSubjectById(id);
		model.addAttribute("subject", subject);
		return "subject_edit";
	}

	@RequestMapping(value = "/edit/{id}", method = RequestMethod.POST)
	public String editSubject(@PathVariable String id,
			@Valid @ModelAttribute Subject subject, BindingResult result,
			Model model) {

		if (result.hasErrors()) {
			return "subject_edit";
		}

		subjectService.updateSubject(subject);
		return "redirect:../edit/" + subject.getSubjectId();
	}

	/**
	 * 
	 * @return JSON method
	 */

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@ResponseBody
	@RequestMapping("/listJSON")
	public JsonObject getAllSubjectJSON() {

		List<Subject> list = new ArrayList<Subject>();

		list = subjectService.getAllSubject();

		// System.out.println(list.size());
		JsonObject subjectJson = new JsonObject();
		subjectJson.setAaData(list);
		subjectJson.setsEcho(1);
		subjectJson.setiTotalDisplayRecords(list.size());
		subjectJson.setiTotalRecords(list.size());
		return subjectJson;
	}
	
	@ResponseBody
	@RequestMapping("/delete")
	public String deleteSelectedUser(@RequestBody String subjects){
		String[] subjectIds = subjects.split(";");
		if(subjectService.deleteSubjects(subjectIds)) {
			return "success";
		} else {
			return "fail";
		}
	}
}
